# -*- coding: utf-8 -*-
"""
Created on Sat Sep 25 10:00:23 2021

@author: 刘长奇-2019300677
"""

import pandas as pd
data = pd.read_csv ("dataset_circles.csv")
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.cluster import DBSCAN
import sklearn.cluster as sc


data = np.array (data)
num = np.shape (data) [0]
x0,x1,y= np.loadtxt("dataset_circles.csv",delimiter=',', usecols=(0,1,2), unpack=True)
x0 = x0.reshape(-1,1)
x1 =x1.reshape(-1,1)
#样本数据的两个维度

y=y.reshape(-1,1)
#样本数据的类别

x = np.hstack((x0. reshape(-1,1) ,x1. reshape(-1, 1)))
#画出没有特征变化的图：
plt.figure()
plt.scatter(x[:,0],x[:,1],c = y)
plt.title('the initial data')
plt.show()
x1=x
x2=x
x3=x
			 
# 调用KMeans方法, 聚类数为2个，fit()之后开始聚类
kmeans = KMeans(n_clusters=2).fit(x)
# 调用DBSCAN方法, eps为最小距离，min_samples 为一个簇中最少的个数，fit()之后开始聚类
dbscan = DBSCAN(eps = 5, min_samples = 5).fit(x1)
#谱聚类方法
SpectralClustering = sc.SpectralClustering(gamma=0.5, n_clusters=2).fit_predict(x2)
model = sc.SpectralClustering(gamma=0.5, n_clusters=2)
#均值中心偏移法
bw = sc.estimate_bandwidth(x, n_samples=len(x), quantile=0.126)
model = sc.MeanShift(bandwidth=bw, bin_seeding=True)
model.fit(x3)  # 完成聚类
pred_y = model.predict(x3) #贴标签

 
# 开始画图
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'm']
markers = ['o', 's', 'D', 'v', '^', 'p', '*', 'o']
plt.subplot(2, 2, 1)
plt.title('kmeans')
for i, l in enumerate(kmeans.labels_):
	plt.plot(x[i,0], x[i,1], color = colors[l], marker = markers[l])
	
plt.subplot(2, 2, 2)
plt.title('dbscan')
for i, l in enumerate(dbscan.labels_):
	plt.plot(x1[i,0], x1[i,1], color = colors[l], marker = markers[l])
    
plt.subplot(2, 2, 3)
plt.title('spectralclustering')
plt.scatter(x2[:, 0], x2[:, 1], c=SpectralClustering)

plt.subplot(2, 2, 4)
plt.title('mean shift')
plt.scatter(x3[:, 0],x3[:, 1], c=pred_y)
	
plt.show()


